clear all
#delimit ;
version 11.2;
set mem 350m;
set matsize 800;
set maxvar 32767;
set more off;
cap log close;


cd "H:\Mi unidad\Grant_new\EFF";


foreach y in 2002{;
use sample_eff.dta, clear;
di "YEAR " "`y'";
keep if year==`y';


cap drop facine3_imp1 facine3_imp2 facine3_imp3 facine3_imp4;
rename facine3_imp5 facine3;
svyset [pw=facine3];

foreach x in 1 2 3 4 5{;



replace hhincome_imp`x'=hhincome_imp`x'*10;
drop if oth_inact_imp`x'==1 | disable_imp`x'==1 | student_imp`x'==1 | housew_imp`x'==1;
keep if age_imp`x'>=3 & age_imp`x' <=7.5;
drop if madult_imp`x'==1;

foreach var in nwealth vhousing vbusloan vcredcard vrestbis {;
replace `var'_imp`x'=`var'_imp`x'/100000;
replace `var'_imp`x'=. if `var'_imp`x'==0;
_pctile `var'_imp`x' [pweight=facine3], p(25,50,75);
return list;

gen perc`var'25_imp`x'=1 if `var'_imp`x'==r(r1);
gen perc`var'50_imp`x'=1 if `var'_imp`x'==r(r2);
gen perc`var'75_imp`x'=1 if `var'_imp`x'==r(r3);

gen `var'25_imp`x' = `var'_imp`x'*perc`var'25_imp`x';
gen `var'50_imp`x' = `var'_imp`x'*perc`var'50_imp`x';
gen `var'75_imp`x' = `var'_imp`x'*perc`var'75_imp`x';
};

/*;*/;
foreach var in  nwealth25_imp`x' nwealth50_imp`x' nwealth75_imp`x' 
vhousing25_imp`x' vhousing50_imp`x' vhousing75_imp`x' 
 vbusloan25_imp`x' vbusloan50_imp`x' vbusloan75_imp`x'
 vcredcard25_imp`x' vcredcard50_imp`x' vcredcard75_imp`x' 
 vrestbis25_imp`x' vrestbis50_imp`x' vrestbis75_imp`x' 
 vportfdebt1_imp`x' vportfdebt2_imp`x' vportfdebt3_imp`x' vportfdebt4_imp`x' vportfdebt12_imp`x' vportfdebt13_imp`x' vportfdebt14_imp`x'
 vportfdebt23_imp`x' vportfdebt24_imp`x' vportfdebt34_imp`x' vportfdebt123_imp`x'
 vportfdebt124_imp`x' vportfdebt134_imp`x' vportfdebt234_imp`x' vportfdebt1234_imp`x' {;

 replace `var'=`var'*acpi_imp1/100 /*IN NOMINAL TERMS*/;
 
};

foreach var in vportfdebt1 vportfdebt2 vportfdebt3 vportfdebt4 vportfdebt12 vportfdebt13 vportfdebt14 vportfdebt23 
 vportfdebt24 vportfdebt34 vportfdebt123 vportfdebt124 vportfdebt134 vportfdebt234 vportfdebt1234 {;
replace `var'_imp`x'=`var'_imp`x'/100000;

};




local j=1;
foreach var in housing_imp`x' busloan_imp`x' credcard_imp`x' restbis_imp`x' nwealth25_imp`x' nwealth50_imp`x' nwealth75_imp`x' 
vhousing25_imp`x' vhousing50_imp`x' vhousing75_imp`x' vbusloan25_imp`x' vbusloan50_imp`x' 
vbusloan75_imp`x' vcredcard25_imp`x' vcredcard50_imp`x' vcredcard75_imp`x' vrestbis25_imp`x' vrestbis50_imp`x' vrestbis75_imp`x'
 portfdebt1_imp`x' portfdebt2_imp`x' portfdebt3_imp`x' portfdebt4_imp`x' portfdebt5_imp`x' portfdebt12_imp`x' portfdebt13_imp`x' portfdebt14_imp`x'
 portfdebt23_imp`x' portfdebt24_imp`x' portfdebt34_imp`x' portfdebt123_imp`x'
 portfdebt124_imp`x' portfdebt134_imp`x' portfdebt234_imp`x' portfdebt1234_imp`x' {;
 
cap svy: mean `var'; 
matrix b0_`j'_imp`x'=e(b);
matrix v0_`j'_imp`x'=e(V);
scalar mean_`var'=b0_`j'_imp`x'[1,1];
scalar var_`var'=v0_`j'_imp`x'[1,1];
scalar sqt_`var'=v0_`j'_imp`x'[1,1]^(1/2);
local j=`j'+1;
};

local j=1;
foreach var in 
 portfdebt1_imp`x' portfdebt2_imp`x' portfdebt3_imp`x' portfdebt4_imp`x' portfdebt12_imp`x' portfdebt13_imp`x' portfdebt14_imp`x'
 portfdebt23_imp`x' portfdebt24_imp`x' portfdebt34_imp`x' portfdebt123_imp`x'
 portfdebt124_imp`x' portfdebt134_imp`x' portfdebt234_imp`x' portfdebt1234_imp`x' {;


summ `var', meanonly;

scalar valid2`var'=r(sum);

if valid2`var'>=1 {;
scalar valid`var'=1 ;
};
else scalar valid`var'=0;

scalar mean_v`var'=0;
scalar var_v`var'=0;
scalar sqt_v`var'=0; 

cap svy: mean v`var'; 

matrix b0_`j'_imp`x'=e(b)*scalar(valid`var');
matrix v0_`j'_imp`x'=e(V)*scalar(valid`var');
scalar mean_v`var'=b0_`j'_imp`x'[1,1]*scalar(valid`var');
scalar var_v`var'=v0_`j'_imp`x'[1,1]*scalar(valid`var');
scalar sqt_v`var'=v0_`j'_imp`x'[1,1]^(1/2)*scalar(valid`var');
local j=`j'+1;
};

};

foreach var in housing busloan credcard restbis nwealth25 nwealth50 nwealth75 vhousing25 vbusloan25 vcredcard25
	vrestbis25 vhousing50 vbusloan50 vcredcard50 vrestbis50 vhousing75 vbusloan75 vcredcard75 vrestbis75 portfdebt1 
	portfdebt2 portfdebt3 portfdebt4 portfdebt12 portfdebt13 portfdebt14 portfdebt23 portfdebt24 portfdebt34 portfdebt123 portfdebt124 portfdebt134  
 portfdebt234 portfdebt1234 vportfdebt1 vportfdebt2 vportfdebt3 vportfdebt4 vportfdebt12 vportfdebt13 vportfdebt14 vportfdebt23 
 vportfdebt24 vportfdebt34 vportfdebt123 vportfdebt124 vportfdebt134 vportfdebt234  vportfdebt1234 {;
	
scalar summean_`var'0 = 0;
scalar sumvar_`var'0 = 0;

forvalues i = 1(1)5 {;
local j = `i' - 1;
scalar summean_`var'`i' = scalar(summean_`var'`j') + scalar(mean_`var'_imp`i');
scalar sumvar_`var'`i' = scalar(sumvar_`var'`j') + scalar(var_`var'_imp`i');
} ;

scalar mean_`var' = (1/5)*scalar(summean_`var'5);
scalar u_`var' = (1/5)*scalar(sumvar_`var'5);
};




foreach x in 1 2 3 4 5{;
local j=1;
foreach var in housing_imp`x' busloan_imp`x' credcard_imp`x' restbis_imp`x' nwealth25_imp`x' nwealth50_imp`x' nwealth75_imp`x' 
vhousing25_imp`x' vhousing50_imp`x' vhousing75_imp`x' 
 vbusloan25_imp`x' vbusloan50_imp`x' vbusloan75_imp`x'
 vcredcard25_imp`x' vcredcard50_imp`x' vcredcard75_imp`x' 
 vrestbis25_imp`x' vrestbis50_imp`x' vrestbis75_imp`x' portfdebt1_imp`x' portfdebt2_imp`x' portfdebt3_imp`x' portfdebt4_imp`x' portfdebt12_imp`x' portfdebt13_imp`x' portfdebt14_imp`x'
 portfdebt23_imp`x' portfdebt24_imp`x' portfdebt34_imp`x' portfdebt123_imp`x'
 portfdebt124_imp`x' portfdebt134_imp`x' portfdebt234_imp`x' portfdebt1234_imp`x'
 vportfdebt1_imp`x' vportfdebt2_imp`x' vportfdebt3_imp`x' vportfdebt4_imp`x' vportfdebt12_imp`x' vportfdebt13_imp`x' vportfdebt14_imp`x'
 vportfdebt23_imp`x' vportfdebt24_imp`x' vportfdebt34_imp`x' vportfdebt123_imp`x'
 vportfdebt124_imp`x' vportfdebt134_imp`x' vportfdebt234_imp`x' vportfdebt1234_imp`x' {;
 
matrix b0_`j'_imp`x'=e(b);
local j=`j'+1;
};

};

foreach x in 1 2 3 4 5{;
local j=1;
foreach var in housing  busloan  credcard  restbis  nwealth25 nwealth50 nwealth75 vhousing25  vhousing50  vhousing75  
vbusloan25  vbusloan50  vbusloan75 
 vcredcard25  vcredcard50  vcredcard75  
 vrestbis25  vrestbis50  vrestbis75  
 portfdebt1 portfdebt2 portfdebt3 portfdebt4 portfdebt12 portfdebt13 portfdebt14 portfdebt23 portfdebt24 portfdebt34 portfdebt123 portfdebt124 portfdebt134  
 portfdebt234 portfdebt1234 vportfdebt1 vportfdebt2 vportfdebt3 vportfdebt4 vportfdebt12 vportfdebt13 vportfdebt14 vportfdebt23 
 vportfdebt24 vportfdebt34 vportfdebt123 vportfdebt124 vportfdebt134 vportfdebt234  vportfdebt1234{;
 
scalar difmeansq_`var'_imp`x'=(b0_`j'_imp`x'[1,1]-scalar(mean_`var'))^2;
local j=`j'+1;

};
};
foreach var in housing  busloan  credcard  restbis  nwealth25 nwealth50 nwealth75 vhousing25  vhousing50  vhousing75  
vbusloan25  vbusloan50  vbusloan75 
 vcredcard25  vcredcard50  vcredcard75  
 vrestbis25  vrestbis50  vrestbis75 
 portfdebt1 portfdebt2 portfdebt3 portfdebt4 portfdebt12 portfdebt13 portfdebt14 portfdebt23 portfdebt24 portfdebt34 portfdebt123 portfdebt124 portfdebt134  
 portfdebt234 portfdebt1234 vportfdebt1 vportfdebt2 vportfdebt3 vportfdebt4 vportfdebt12 vportfdebt13 vportfdebt14 vportfdebt23 
 vportfdebt24 vportfdebt34 vportfdebt123 vportfdebt124 vportfdebt134 vportfdebt234  vportfdebt1234{;

scalar sumdifmeansq_`var'0 = 0;

forvalues i = 1(1)5 {;
local j = `i' - 1;
scalar sumdifmeansq_`var'`i' = sumdifmeansq_`var'`j' + difmeansq_`var'_imp`i';
} ;

scalar b_`var'=(1/4)*sumdifmeansq_`var'5;
scalar t_`var'=scalar(u_`var')+(6/5)*scalar(b_`var');
scalar sqt_`var'=scalar(t_`var')^(1/2);
};





di "Year" "`y'";
di "# observations";
count;

foreach var in nwealth25 nwealth50 nwealth75 housing vhousing25 vhousing50 vhousing75
 busloan vbusloan25  vbusloan50 vbusloan75 credcard vcredcard25 vcredcard50 vcredcard75 restbis vrestbis25 vrestbis50 vrestbis75
 portfdebt1 portfdebt2 portfdebt3 portfdebt4 portfdebt12 portfdebt13 portfdebt14 portfdebt23 portfdebt24 portfdebt34 portfdebt123 portfdebt124 portfdebt134  
 portfdebt234 portfdebt1234 vportfdebt1 vportfdebt2 vportfdebt3 vportfdebt4 vportfdebt12 vportfdebt13 vportfdebt14 vportfdebt23 
 vportfdebt24 vportfdebt34 vportfdebt123 vportfdebt124 vportfdebt134 vportfdebt234  vportfdebt1234

{;

di "`var'";
di %3.2f scalar(mean_`var')*100;
*di %3.2f scalar(sqt_`var')*100;


};	



};
